<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="JavaScript," />










<meta name="description" content="本文读者将从以下几个方面讲述队列的JS实现  什么是队列？ 顺序队列的实现 链队列的实现  这里不对循环队列做介绍，如果有兴趣，请自行探索 1.什么是队列？队列是一种操作受限的线性表 队列遵循FIFO原则,即先进先出原则 就像我们平时排队一样，先排队的当然最先享受到服务 这里有几个概念需要解释 队首&#x2F;队头：队列的首结点（元素），最先入队的元素&#x2F;结点 队尾：队列的尾结点（元素），最后入队的元素&#x2F;结点">
<meta property="og:type" content="article">
<meta property="og:title" content="JS实现队列">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="本文读者将从以下几个方面讲述队列的JS实现  什么是队列？ 顺序队列的实现 链队列的实现  这里不对循环队列做介绍，如果有兴趣，请自行探索 1.什么是队列？队列是一种操作受限的线性表 队列遵循FIFO原则,即先进先出原则 就像我们平时排队一样，先排队的当然最先享受到服务 这里有几个概念需要解释 队首&#x2F;队头：队列的首结点（元素），最先入队的元素&#x2F;结点 队尾：队列的尾结点（元素），最后入队的元素&#x2F;结点">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/1.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/2.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/3.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/4.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/5.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/6.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/7.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/8.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/9.png">
<meta property="article:published_time" content="2022-07-11T12:42:41.000Z">
<meta property="article:modified_time" content="2022-07-11T15:08:24.769Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/07/11/JS实现队列/"/>





  <title>JS实现队列 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JS实现队列</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-07-11T20:42:41+08:00">
                2022-07-11
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/07/11/JS实现队列/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文读者将从以下几个方面讲述队列的JS实现</p>
<ul>
<li>什么是队列？</li>
<li>顺序队列的实现</li>
<li>链队列的实现</li>
</ul>
<p>这里不对循环队列做介绍，如果有兴趣，请自行探索</p>
<h2 id="1-什么是队列？"><a href="#1-什么是队列？" class="headerlink" title="1.什么是队列？"></a>1.什么是队列？</h2><p>队列是一种操作受限的线性表</p>
<p>队列遵循<strong>FIFO</strong>原则,即先进先出原则</p>
<p>就像我们平时排队一样，先排队的当然最先享受到服务</p>
<p>这里有几个概念需要解释</p>
<p>队首/队头：队列的首结点（元素），最先入队的元素/结点</p>
<p>队尾：队列的尾结点（元素），最后入队的元素/结点</p>
<p>入队：把新元素或者新的结点进入队列</p>
<p>出队：让队首元素出队列</p>
<p>和栈（上上篇文章详细介绍过）不一样，栈是先进后出，队列是先进先出</p>
<p>队列示意图如下图所示：</p>
<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/1.png" class title="队列的结构示意图">

<span id="more"></span>

<h2 id="2-顺序队列"><a href="#2-顺序队列" class="headerlink" title="2.顺序队列"></a>2.顺序队列</h2><p>顺序队列读者在这里使用数组实现</p>
<p>使用JS数组的push()方法代替入队操作</p>
<p>使用JS数组的shift()方法代替出队操作</p>
<p>具体如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@name</span>:myQueue</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>:louchao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>:2022/7/11 21:01</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OrderQueue</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.dataStore = [];</span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 入队操作</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param <span class="variable">element</span></span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;boolean&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">enQueue</span>(<span class="params">element</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.dataStore.push(element);</span><br><span class="line">        <span class="built_in">this</span>.length++;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 出队操作</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">deQueue</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.length&lt;=<span class="number">0</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;out of range&quot;</span>);</span><br><span class="line">        <span class="built_in">this</span>.dataStore.shift();</span><br><span class="line">        <span class="built_in">this</span>.length--;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断队列是否为空</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;boolean&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">isEmpty</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length === <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取队列长度</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;number&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">getSize</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取队首元素</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">peek</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.dataStore[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取队尾元素</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">getRear</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.dataStore[<span class="built_in">this</span>.length-<span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 清空队列</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;*[]&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">clear</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.length; i++) &#123;</span><br><span class="line">            <span class="built_in">this</span>.dataStore.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> oq = <span class="keyword">new</span> OrderQueue();</span><br><span class="line">oq.enQueue(<span class="string">&quot;zs&quot;</span>);</span><br><span class="line">oq.enQueue(<span class="string">&quot;ls&quot;</span>);</span><br><span class="line">oq.enQueue(<span class="string">&quot;ww&quot;</span>);</span><br><span class="line">oq.enQueue(<span class="string">&quot;zq&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(oq);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;获取队列长度：&quot;</span>,oq.getSize());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;获取队头元素：&quot;</span>,oq.peek());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;获取队尾元素：&quot;</span>,oq.getRear());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;判断队列为空：&quot;</span>,oq.isEmpty());</span><br><span class="line">oq.deQueue();</span><br><span class="line"><span class="built_in">console</span>.log(oq);</span><br><span class="line">oq.clear();</span><br><span class="line"><span class="built_in">console</span>.log(oq);</span><br></pre></td></tr></table></figure>

<p>结果如下：</p>
<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/2.png" class title="顺序队列">

<h2 id="3-链队列"><a href="#3-链队列" class="headerlink" title="3.链队列"></a>3.链队列</h2><p>基于链表实现的队列</p>
<p>这样的队列结构示意图如下</p>
<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/3.png" class title="链式队列结构示意图">

<p>具体如何实现，请看下文</p>
<p>首先链式队列本质上是操作受限的链表，因此基本单位都是由结点组成，结点的基本数据结构为</p>
<p><strong>1.结点和队列基本结构</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义结点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">element</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = element;</span><br><span class="line">        <span class="built_in">this</span>.next = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 队列基本结构</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkedQueue</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.front = <span class="literal">null</span>;      <span class="comment">//队首</span></span><br><span class="line">        <span class="built_in">this</span>.rear = <span class="literal">null</span>;       <span class="comment">//队尾</span></span><br><span class="line">        <span class="built_in">this</span>.data = <span class="literal">null</span>;       <span class="comment">//队列数据域</span></span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span>;        <span class="comment">//队列长度</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>2.入队操作</strong></p>
<p>入队示意图如下</p>
<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/4.png" class title="链式队列入队过程">

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 入队操作</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="variable">element</span></span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;boolean&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">enQueue</span>(<span class="params">element</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> node = <span class="keyword">new</span> Node(element);</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">this</span>.length === <span class="number">0</span>)&#123;         <span class="comment">//如果是个空队列</span></span><br><span class="line">        <span class="built_in">this</span>.front = node;          <span class="comment">//队头指向新建节点</span></span><br><span class="line">        <span class="built_in">this</span>.rear = node;           <span class="comment">//队尾指向新建节点</span></span><br><span class="line">        <span class="built_in">this</span>.length++;              <span class="comment">//队列长度+1</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> current = <span class="built_in">this</span>.rear;    <span class="comment">//current用作临时变量缓存队尾，且指向队尾</span></span><br><span class="line">        current.next = node;        <span class="comment">//临时结点指向新结点</span></span><br><span class="line">        <span class="built_in">this</span>.rear = node;           <span class="comment">//队尾结点指向新结点</span></span><br><span class="line">        <span class="built_in">this</span>.length++;              <span class="comment">//队列长度+1</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> lq = <span class="keyword">new</span> LinkedQueue();</span><br><span class="line">lq.enQueue(<span class="string">&quot;zs&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ls&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ww&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;zq&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>测试结果如下</p>
<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/5.png" class title="链式队列入队结果">

<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/6.png" class title="链式队列入队结果">

<p><strong>3.出队操作</strong></p>
<p>出队流程</p>
<ul>
<li>current用作临时变量缓存队头，且指向队头</li>
<li>队头往下移动一次</li>
<li>队列长度-1</li>
<li>返回出队的数据</li>
</ul>
<p>出队示意图：</p>
<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/7.png" class title="链式队列出队示意图">

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 出队操作</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">deQueue</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">this</span>.length)&#123;                  <span class="comment">//越界</span></span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;out of range!!&quot;</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> current = <span class="built_in">this</span>.front;       <span class="comment">//current用作临时变量缓存队头，且指向队头</span></span><br><span class="line">        <span class="built_in">this</span>.front = current.next;      <span class="comment">//队头往下移动一次</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.front===<span class="literal">null</span>)&#123;         <span class="comment">//如果队列为空</span></span><br><span class="line">            <span class="built_in">this</span>.rear = <span class="literal">null</span>;           <span class="comment">//把队尾也置空</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length--;                  <span class="comment">//队列长度-1</span></span><br><span class="line">        <span class="keyword">return</span> current.data;            <span class="comment">//返回出队的数据</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> lq = <span class="keyword">new</span> LinkedQueue();</span><br><span class="line">lq.enQueue(<span class="string">&quot;zs&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ls&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ww&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;zq&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line"><span class="built_in">console</span>.log(lq);</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/8.png" class title="链式队列出队结果">

<p><strong>4.获取队首元素</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取队首元素</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">peek</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.front.data;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>5.获取队尾元素</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取队尾元素</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">getRear</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.rear.data;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>6.获取队列长度</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取队列长度</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;number&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">getSize</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.length;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>7.判断队列是否为空</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断队列是否为空</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;boolean&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">isEmpty</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.length === <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>8.清空队列</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 清空队列</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="title">clear</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.front = <span class="literal">null</span>;</span><br><span class="line">    <span class="built_in">this</span>.length = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> lq = <span class="keyword">new</span> LinkedQueue();</span><br><span class="line">lq.enQueue(<span class="string">&quot;zs&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ls&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ww&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;zq&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队首元素&quot;</span>,lq.peek());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队尾元素&quot;</span>,lq.getRear());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队列长度&quot;</span>,lq.getSize());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队列为空？&quot;</span>,lq.isEmpty());</span><br><span class="line">lq.clear();</span><br><span class="line"><span class="built_in">console</span>.log(lq);</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/9.png" class title="链式队列测试操作结果">

<h2 id="4-链式队列操作实现所有代码"><a href="#4-链式队列操作实现所有代码" class="headerlink" title="4.链式队列操作实现所有代码"></a>4.链式队列操作实现所有代码</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义结点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">element</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = element;</span><br><span class="line">        <span class="built_in">this</span>.next = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 队列基本结构</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkedQueue</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.front = <span class="literal">null</span>;      <span class="comment">//队首</span></span><br><span class="line">        <span class="built_in">this</span>.rear = <span class="literal">null</span>;       <span class="comment">//队尾</span></span><br><span class="line">        <span class="built_in">this</span>.data = <span class="literal">null</span>;       <span class="comment">//队列数据域</span></span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span>;        <span class="comment">//队列长度</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 入队操作</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param <span class="variable">element</span></span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;boolean&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">enQueue</span>(<span class="params">element</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> node = <span class="keyword">new</span> Node(element);</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.length === <span class="number">0</span>)&#123;         <span class="comment">//如果是个空队列</span></span><br><span class="line">            <span class="built_in">this</span>.front = node;          <span class="comment">//队头指向新建节点</span></span><br><span class="line">            <span class="built_in">this</span>.rear = node;           <span class="comment">//队尾指向新建节点</span></span><br><span class="line">            <span class="built_in">this</span>.length++;              <span class="comment">//队列长度+1</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> current = <span class="built_in">this</span>.rear;    <span class="comment">//current用作临时变量缓存队尾，且指向队尾</span></span><br><span class="line">            current.next = node;        <span class="comment">//临时结点指向新结点</span></span><br><span class="line">            <span class="built_in">this</span>.rear = node;           <span class="comment">//队尾结点指向新结点</span></span><br><span class="line">            <span class="built_in">this</span>.length++;              <span class="comment">//队列长度+1</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 出队操作</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">deQueue</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">this</span>.length)&#123;                  <span class="comment">//越界</span></span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;out of range!!&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> current = <span class="built_in">this</span>.front;       <span class="comment">//current用作临时变量缓存队头，且指向队头</span></span><br><span class="line">            <span class="built_in">this</span>.front = current.next;      <span class="comment">//队头往下移动一次</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">this</span>.front===<span class="literal">null</span>)&#123;         <span class="comment">//如果队列为空</span></span><br><span class="line">                <span class="built_in">this</span>.rear = <span class="literal">null</span>;           <span class="comment">//把队尾也置空</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">this</span>.length--;                  <span class="comment">//队列长度-1</span></span><br><span class="line">            <span class="keyword">return</span> current.data;            <span class="comment">//返回出队的数据</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取队首元素</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">peek</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.front.data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取队尾元素</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;*&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">getRear</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.rear.data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取队列长度</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;number&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">getSize</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断队列是否为空</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@returns <span class="type">&#123;boolean&#125;</span></span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">isEmpty</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length === <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 清空队列</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="title">clear</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.front = <span class="literal">null</span>;</span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> lq = <span class="keyword">new</span> LinkedQueue();</span><br><span class="line">lq.enQueue(<span class="string">&quot;zs&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ls&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;ww&quot;</span>);</span><br><span class="line">lq.enQueue(<span class="string">&quot;zq&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队首元素&quot;</span>,lq.peek());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队尾元素&quot;</span>,lq.getRear());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队列长度&quot;</span>,lq.getSize());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;队列为空？&quot;</span>,lq.isEmpty());</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line"><span class="built_in">console</span>.log(lq.deQueue());</span><br><span class="line">lq.clear();</span><br><span class="line"><span class="built_in">console</span>.log(lq);</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/" rel="next" title="JS事件循环机制">
                <i class="fa fa-chevron-left"></i> JS事件循环机制
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/07/13/JS%E6%9E%84%E5%BB%BA%E4%BB%A5%E5%8F%8A%E9%81%8D%E5%8E%86%E4%BA%8C%E5%8F%89%E6%A0%91/" rel="prev" title="JS构建以及遍历二叉树">
                JS构建以及遍历二叉树 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E9%98%9F%E5%88%97%EF%BC%9F"><span class="nav-number">1.</span> <span class="nav-text">1.什么是队列？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E9%A1%BA%E5%BA%8F%E9%98%9F%E5%88%97"><span class="nav-number">2.</span> <span class="nav-text">2.顺序队列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%93%BE%E9%98%9F%E5%88%97"><span class="nav-number">3.</span> <span class="nav-text">3.链队列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E9%93%BE%E5%BC%8F%E9%98%9F%E5%88%97%E6%93%8D%E4%BD%9C%E5%AE%9E%E7%8E%B0%E6%89%80%E6%9C%89%E4%BB%A3%E7%A0%81"><span class="nav-number">4.</span> <span class="nav-text">4.链式队列操作实现所有代码</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/';
          this.page.identifier = '2022/07/11/JS实现队列/';
          this.page.title = 'JS实现队列';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
